package com.example.beijing.beijingdemo.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.example.beijing.beijingdemo.base.NewsCenterTabPager;
import com.example.beijing.beijingdemo.bean.NewsCenterBean;
import com.example.beijing.beijingdemo.util.Constants;

import java.util.List;

/**
 * 新闻页面的Tab的Adapter
 */

public class NewsCenterTabVPAdapter extends PagerAdapter {

    private List<NewsCenterTabPager> views;
    private List<NewsCenterBean.NewsCenterNewsTabBean> mTabBeanList;

    /**
     * 构造函数
     */
    public NewsCenterTabVPAdapter(List<NewsCenterTabPager> views, List<NewsCenterBean.NewsCenterNewsTabBean> mTabBeanList) {
        this.views = views;
        this.mTabBeanList = mTabBeanList;
    }

    @Override
    public int getCount() {
        return views == null ? 0 : views.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(position).view;
        container.addView(view);

        // 进行数据的加载
        NewsCenterTabPager tabPager = views.get(position);
        String url = Constants.URL_HOST + mTabBeanList.get(position).url;
        tabPager.loadNetData(url);

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTabBeanList.get(position).title;
    }
}
